home *** CD-ROM | disk | FTP | other *** search
- Path: kuhub.cc.ukans.edu!anh
- From: anh@kuhub.cc.ukans.edu (TRAN PHAN ANH)
- Newsgroups: comp.lang.c
- Subject: Re: reversing a string
- Message-ID: <1996Apr9.144011.117444@kuhub.cc.ukans.edu>
- Date: 9 Apr 96 14:40:11 CDT
- References: <4k6cjl$j8f@central.server.swt.edu> <4kb1s7$6eu@ibm32.perftech.com> <829058514snz@genesis.demon.co.uk>
- Organization: University of Kansas Academic Computing Services
-
- Something like this? Or is the s2 argument considered an extra variable.
-
- Anh
-
-
- #include <stdio.h>
- #include <string.h>
-
- void reverse(char *s1, char *s2)
- {
- if(s1>=s2)
- return;
-
- *s1^=*s2;
- *s2^=*s1;
- *s1^=*s2;
-
- reverse(s1+1,s2-1);
- }
-
-
- int main(int argc, char *argv[])
- {
- printf("Arg: %s\n",argv[1]);
-
- reverse(argv[1],&argv[1][strlen(argv[1])-1]);
-
- printf("Result: %s\n",argv[1]);
- }
-
-
-
-
-
- In article <829058514snz@genesis.demon.co.uk>, Lawrence Kirby <fred@genesis.demon.co.uk> writes:
- > In article <4kb1s7$6eu@ibm32.perftech.com>
- > murf@perftech.com "John Murphy" writes:
- >
- >>In article <4k6cjl$j8f@central.server.swt.edu>, ln16674@nyssa.swt.edu
- >>says...
- >>>
- >>>I have a challenge from a friend of mine. He wanted me to reverse a string
- >>>with recursion without using any additional variables or loops. I got mine
- >>>to work by using exclusive or, but I needed an additional variable. Can
- >>>someone help with this problem without using the additional variable?
- >>>
- >>>Thanks
- >>You can swap two variables, x and y, with the following series of exclusive
- >>or's:
- >> x ^= y;
- >> y ^= x;
- >> x ^= y;
- >
- > From what he wrote I believe Leland was already doing that. The problem is
- > to write the entire function without using an additional variable. It
- > can be done! :-)
- >
- > --
- > -----------------------------------------
- > Lawrence Kirby | fred@genesis.demon.co.uk
- > Wilts, England | 70734.126@compuserve.com
- > -----------------------------------------
-